package com.seleniti.enpareja.web.desktop.client.ui.widget;

import org.ioc.commons.ui.HasValue;
import org.ioc.commons.ui.features.Info;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.ImportedWithPrefix;
import com.google.gwt.resources.client.CssResource.Shared;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.seleniti.enpareja.web.desktop.client.ui.resources.CommonResources;

public class InfoPopupWidget extends Composite implements Info {

	private static TemplateUiBinder uiBinder = GWT.create(TemplateUiBinder.class);

	interface TemplateUiBinder extends UiBinder<Widget, InfoPopupWidget> {
	}

	@Shared
	@ImportedWithPrefix("infoPopup")
	public static interface InfoPopupStyle extends CssResource {
		String popupContainer();

		String content();

		String message();
	}

	private HasValue<String> message;
	private InfoType type;
	private final DialogBox dialog = new DialogBox();

	@UiField
	InfoPopupStyle style;

	@UiField
	HTML lblMessage;

	@UiField
	Image imgIcon;

	@UiField
	CommonResources common;

	public InfoPopupWidget(String captionText, InfoType type) {
		this(captionText);
		this.setType(type);
	}

	public InfoPopupWidget(String captionText) {
		this();
		this.setCaptionText(captionText);
	}

	public InfoPopupWidget() {

		initWidget(uiBinder.createAndBindUi(this));

		this.dialog.setAnimationEnabled(true);
		this.dialog.setGlassEnabled(true);
		this.dialog.setModal(true);
		this.dialog.setAutoHideEnabled(true);
		this.dialog.setAutoHideOnHistoryEventsEnabled(true);

		this.dialog.addStyleName(style.popupContainer());
		this.dialog.setGlassStyleName(common.style().glass());

		this.setType(InfoType.INFO);

		this.dialog.setWidget(this);
	}

	@UiHandler("btnOk")
	void onOkButtonClicked(ClickEvent evt) {
		this.setVisible(false);
	}

	public void setCaptionText(String text) {
		this.dialog.getCaption().setText(text);
	}

	public void setMessage(String message) {
		setMessage(message, true);
	}

	public void setMessage(String message, boolean asHtml) {
		if (asHtml) {
			lblMessage.setHTML(message);
		} else {
			lblMessage.setText(message);
		}
	}

	public void setMessage(SafeHtml message) {
		lblMessage.setHTML(message);
	}

	public String getMesssage() {
		return lblMessage.getText();
	}

	@Override
	public void setType(InfoType infoType) {
		this.type = infoType;

		switch (infoType) {
		case INFO:
			imgIcon.setResource(common.info32x32());
			break;
		case ERROR:
			imgIcon.setResource(common.error32x32());
			break;
		case OPERATION_OK:
			imgIcon.setResource(common.tick32x32());
			break;
		case WARNING:
			imgIcon.setResource(common.warning32x32());
			break;
		}
	}

	@Override
	public InfoType getType() {
		return this.type;
	}

	public HasValue<String> message() {
		if (this.message == null) {
			this.message = new HasValue<String>() {

				public void setValue(String text) {
					setMessage(text);
				}

				public String getValue() {
					return getMesssage();
				}
			};
		}
		return this.message;
	}

	@Override
	public void setVisible(boolean visible) {
		if (visible) {
			this.dialog.show();
		} else {
			this.dialog.hide();
		}
	}

	@Override
	public boolean isVisible() {
		return this.dialog.isShowing();
	}

	public void center() {
		this.dialog.center();
	}

	public String getText() {
		return lblMessage.getText();
	}

	public void setText(String text) {
		lblMessage.setText(text);
	}
}
